﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DAL.Infrastructure;
using DAL.Interfaces;
using DAL.Models;
using BAL.Infrastructure;
using BAL.Interfaces;
using System.Data;
using System.Data.Entity;
using System.Web;

namespace BAL
{
    public class BALMaterials : ServiceBase<Materials>,  IMaterialsService
    {

        public BALMaterials(IRepository<Materials> repository, IApplicationContext context) 
            : base(repository, context)
        { 
                    
        }

        public IQueryable<Materials> Buscar(BibliotecaVirtual.Models.Buscar model)
        {
            IQueryable<Materials> result = null;
            try
            {
                result = _context.Materiales.Include(x => x.Autors).Include(x => x.Editorials).
                    Include(x => x.Ejemplars).Include(x => x.Reservaciones).Include(x => x.TemasMateriales).
                    Include(x => x.TipoMaterials)
                    .Where(x => x.Titulo.Contains(model.Titulo) || x.Codigo.Contains(model.Codigo) 
                        || x.Ejemplars.Any(y => y.Codigo.Contains(model.Codigo)) || 
                        x.Autors.Nombre.Contains(model.Autor) || x.Autors.Apellido.Contains(model.Autor) || 
                        x.TemasMateriales.Any(z => z.Temas.Nombre.Contains(model.Tema)));

                foreach (Materials material in result)
                {
                    material.CantidadEjemplares = material.Ejemplars.Where(x => x.MaterialId == material.id).Count();
                    material.CantidadEjemplaresDisponibles = material.Ejemplars.Where(x => x.MaterialId == material.id && x.EstadoEjemplars.Descripcion == "DISPONIBLE").Count();
                }
            }
            catch (Exception ex)
            {

            }
            return result;
        }

        public override IQueryable<Materials> GetAllValidados()
        {
            IQueryable<Materials> result = null;
            try
            {
                result = _context.Materiales.Include(x => x.Autors).Include(x => x.Editorials).Include(x => x.Ejemplars).Include(x => x.Reservaciones).Include(x => x.Temas).Include(x => x.TemasMateriales).Include(x => x.TipoMaterials);

                foreach (Materials material in result){
                    material.CantidadEjemplares = material.Ejemplars.Where(x => x.MaterialId == material.id).Count();
                    material.CantidadEjemplaresDisponibles = material.Ejemplars.Where(x => x.MaterialId == material.id && x.EstadoEjemplars.Descripcion == "DISPONIBLE").Count();
                }

            }
            catch (Exception ex)
            {
            }


            return result;
        }

        public Materials GetByID(long id)
        {
            Materials result = new Materials();
            try
            {
                result = _context.Materiales.Include(x => x.Autors).Include(x => x.Editorials).Include(x => x.Ejemplars).Include(x => x.Reservaciones).Include(x => x.Temas).Include(x => x.TemasMateriales).Include(x => x.TipoMaterials).Where(x => x.id == id).First();
                result.CantidadEjemplares = result.Ejemplars.Where(x => x.MaterialId == result.id).Count();
                result.CantidadEjemplaresDisponibles = result.Ejemplars.Where(x => x.MaterialId == result.id && x.EstadoEjemplars.Descripcion == "DISPONIBLE").Count();

            }
            catch (Exception ex)
            {

            }
            return result;
        }

    }
}
